package org.opengeoingest.tufts.Layer;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Hashtable;
import java.util.Set;
import java.util.Vector;

/**
 * read in a file with thesaurus data and write it out in Solr synonym.txt format 
 * It is not currently used.
 * @author smcdon08
 *
 */
public class ThesaurusProcessor
{

	public static Hashtable<String, Vector<String>> readFile(String inputFileName) throws IOException
	{
		BufferedReader inputFile = new BufferedReader(new FileReader(inputFileName));
		
		Hashtable<String, Vector<String>> keywords = new Hashtable<String, Vector<String>>();
		inputFile.readLine();  // file starts with header, ignore that line
		String currentLine = inputFile.readLine();
		while (currentLine != null)
		{
			String[] parts = currentLine.split("\t");
			//System.out.println("line: " + currentLine +", parts length = " + parts.length);
			String category = parts[categoryIndex].trim();
			String word1 = parts[word1Index].trim();
			String word2 = "";
			if (parts.length > 2)
				word2 = parts[word2Index].trim();
			if ((category.contains(" ") == false) && (word1.contains(" ") == false) && (word1.contains(".") == false) && (word2.contains(" ") == false) && (word1.contains(".") == false))
			{
				// we only are dealing with single words, not phrases with multiple words
				Vector<String> currentVector; 
				if (keywords.containsKey(category) == true)
				{
					currentVector = keywords.get(category);

				}
				else
				{
					currentVector = new Vector<String>();
				}
					
				currentVector.add(word1);
				if (word2.length() > 0)
					currentVector.add(word2);
				keywords.put(category, currentVector);				
				System.out.println("added " + category + ", " + word1 + ", " + word2 + ", " + currentVector.size());
			}
			currentLine = inputFile.readLine();
		}
		return keywords;	
	}
	
	public static void saveFile(Hashtable<String, Vector<String>> keywords, String outputFileName) throws FileNotFoundException
	{
		FileOutputStream tempStream = new FileOutputStream(outputFileName);
		PrintStream outputStream = new PrintStream(tempStream);
		Set<String> hashKeys = keywords.keySet();
		for (String currentKey : hashKeys)
		{
			outputStream.print(currentKey);
			System.out.print(currentKey);
			Vector<String> words = keywords.get(currentKey);
			for (String currentWord : words)
			{
				outputStream.print("," + currentWord);
				System.out.print("," + currentWord);
			}
			outputStream.println();
			outputStream.println();
			System.out.println();
		}
	}
	
	//public static String inputFileName = "/Users/smcdon08/tmp/OpenGeoPortal/thesaurus/ProcessedData/ForTestingISO.txt";
	public static String inputFileName = "/Users/smcdon08/tmp/OpenGeoPortal/thesaurus/ProcessedData/ForTestingLCSH.txt";
	public static String outputFileName = "/Users/smcdon08/tmp/OpenGeoPortal/thesaurus/ProcessedData/SolrDataLchs.txt";
	
	public static int categoryIndex = 1;
	public static int word1Index = 0;
	public static int word2Index = 2;
	
	
	public static void main(String[] args) throws IOException
	{
		Hashtable<String, Vector<String>> keywords = readFile(inputFileName);
		saveFile(keywords, outputFileName);
	}
}
